<template>
  <div class="settings">
    <el-card>
      <div slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>个人设置</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-row>
        <el-col :span="12">
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="编号:">{{ form.id }}</el-form-item>
            <el-form-item label="手机:">{{ form.mobile }}</el-form-item>
            <el-form-item label="媒体名称:">
              <el-input maxlength="7" v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="媒体简介:">
              <el-input type="textarea" v-model="form.intro"></el-input>
            </el-form-item>
            <el-form-item label="联系邮箱:">
              <el-input v-model="form.email"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">保存设置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <el-upload
            class="avatar-uploader"
            action=""
            :show-file-list="false"
            :http-request="uploadPic"
          >
            <img v-if="form.photo" :src="form.photo" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { userGetProfile, modUserProfile, moduserImage } from '../../api/user'
import eventBus from '../../../eventBus'
import { setUser, getUser } from '../../utils/storage'
export default {
  name: 'Settings',
  created () {
    userGetProfile().then(res => {
      console.log(res)
      // userLsit = res.data.data
      // const userList = ''
      // userList = res.dataS
      // this.$store.state.userInfo = res.data.data
      this.form = res.data.data
      this.imageUrl = res.data.data.photo
    })
  },
  data () {
    return {
      form: {
        id: '',
        mobile: '',
        name: '',
        intro: '',
        email: '',
        photo: ''
      }
    }
  },
  methods: {
    uploadPic ({ file }) {
      // console.log(file)
      const formData = new FormData()
      formData.append('photo', file)
      moduserImage(formData).then(res => {
        console.log(res)
        this.$message.success('上传头像成功!')
        this.form.photo = res.data.data.photo
        eventBus.$emit('update-user-photo', res.data.data.photo)
        // 存储到本地
        const localUser = getUser()
        localUser.photo = res.data.data.photo
        setUser(localUser)
      })
    },
    submit () {
      const { name, intro, email } = this.form
      modUserProfile({ name, intro, email }).then(res => {
        eventBus.$emit('update-user-name', name)
        // 同步本地存储的用户名称
        // 拿出数据
        const localUser = getUser()
        // 修改数据
        localUser.name = name
        // 存储数据
        setUser(localUser)
        this.$message.success('修改成功!')
        console.log(res)
      })
    }
  }
}
</script>

<style lang="less">
.settings {
  .el-card {
    height: 100vh;
    .el-col {
      .avatar-uploader {
        text-align: center;
      }
      .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }
      .avatar-uploader .el-upload:hover {
        border-color: #409eff;
      }
      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
      }
      .avatar {
        width: 178px;
        height: 178px;
        display: block;
      }
    }
  }
}
</style>
